package com.wanpinghui.supplier.business.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.VideoView;

import com.wanpinghui.wphlibrary.common.toast.ToastManager;
import com.yunshl.yunshllibrary.manager.UpdateManager;
import com.yunshl.yunshllibrary.storage.ShareDataManager;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.io.File;
import java.io.IOException;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2018/2/6
 ***/

public class WVideoView extends VideoView {
    private boolean isPrepare;
    private boolean isStartOpen;
    public WVideoView(Context context) {
        super(context);
    }

    public WVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public WVideoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void start() {
        if (isPrepare) {
            super.start();
            isStartOpen = false;
        }else {
            isStartOpen = true;
        }
    }

    public void setVideoPath(String url){
        String path = ShareDataManager.getInstance().getPara(TextUtil.md5Lower(url));
        if (TextUtil.isNotEmpty(path)) {
            File file = new File(path);
            if (file.exists()) {
                isPrepare = true;
                WVideoView.this.setVideoURI(Uri.fromFile(file));
            }else {
                ShareDataManager.getInstance().delete(getContext(),TextUtil.md5Lower(url));
                downLoad(url);
            }
        }else {
            downLoad(url);
        }
    }

    public void downLoad(final String url) {
        UpdateManager manager = UpdateManager.newInstance();
        manager.setDownloadStatus(new UpdateManager.IDownloadStatus() {
            @Override
            public void downloadFinish(String fileName) throws IOException {
                ShareDataManager.getInstance().save(getContext(), TextUtil.md5Lower(url),fileName);
                File file = new File(fileName);
                if (file.exists()) {
                    WVideoView.this.setVideoURI(Uri.fromFile(file));
                    isPrepare = true;
                    if (isStartOpen) {
                        start();
                    }
                }
            }

            @Override
            public void downloadPause() {

            }

            @Override
            public void downloadStart(long fileLength) {

            }

            @Override
            public void downLoading(int progress, int second) {

            }

            @Override
            public void downLoadError(String message) {
                ToastManager.getInstance().showToast("播放出错["+message+"]");
            }
        });
        manager.downLoad(url, getContext().getFilesDir().getAbsolutePath()+"/video",url.substring(url.lastIndexOf(".")+1));
    }
}
